package work;

public class DataCompression {
    private int[] arr;
    private int arr_size;


    public DataCompression(int[][] array) {
        arr_size = array.length;
        arr = new int[arr_size * (1 + arr_size) / 2];
        int index = 0;

        for (int i = 0; i < arr_size; i++) {
            for (int j = 0; j < arr_size; j++) {
                if (array[i][j] != 0) {
                    arr[index++] = array[i][j];
                }
            }
        }
    }

    /***
     *
     * @param args (1+n)*n/2
     */
    public static void main(String[] args) {
        int[][] array = {
                {1, 0, 0, 0},
                {1, 3, 0, 0},
                {2, 4, 6, 0},
                {2, 4, 7, 9}
        };
        DataCompression dataCompression=new DataCompression(array);
        int i=0,j=0;
        System.out.println("===========================");
        for (int k = 0; k <dataCompression.arr_size ; k++) {
            for (int l = 0; l <dataCompression.arr_size ; l++) {
                System.out.print("\t"+array[i][j]);
                System.out.println();
            }
        }

        System.out.println("=========================================");
        System.out.print("\t"+"[");
        for (int k = 0; k <dataCompression.arr_size ; k++) {
            for (int l = 0; l <dataCompression.arr_size ; l++) {
                System.out.print(" "+dataCompression.getValue(i,j));
            }
            System.out.print(" ]");
            System.out.println();
        }
    }

    /***
     *
     * @param i
     * @param j
     * @return
     */
    public int getValue(int i, int j) {
        int index = arr_size * i - i * (i + 1) / 2 + j;
        return arr[index];
    }
}
